package com.mida.app.report.service.impl;

import com.mida.app.report.service.DocReportServiceLockService;
import com.mida.app.report.vo.DocReportListVo;
import com.mida.bms.report.api.DocReportServiceLockApi;
import com.mida.bms.report.entity.DocReportServiceLock;
import com.mida.dms.mdm.dto.UserDto;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: DocReportServiceLockServiceImpl
 * @Description: TODO
 * @Author: wang xiao le
 * @Date: 2021/8/9 14:48
 **/
@Service
@Slf4j
public class DocReportServiceLockServiceImpl implements DocReportServiceLockService {

    public static final long LOCK = 45989833557934080L;
    public static final long UNLOCK = 45989877979807744L;

    @Autowired
    private DocReportServiceLockApi docReportServiceLockApi;

    @Override
    public void saveLock(DocReportServiceLock docReportServiceLock, UserDto userDto) {
        //插入锁表
        Date date = new Date();
        docReportServiceLock.setStatus(LOCK);
        docReportServiceLock.setLockUserSoid(userDto.getEmployeeSoid());
        docReportServiceLock.setLockDateTime(date);
        docReportServiceLock.setCreator(userDto.getEmployeeSoid());
        docReportServiceLock.setCreateDate(date);
        docReportServiceLock.setUpdateUser(userDto.getEmployeeSoid());
        docReportServiceLock.setUpdateDate(date);
        docReportServiceLock.setIsDelete(0L);
        docReportServiceLockApi.insert(docReportServiceLock);
    }

    @Override
    public ActionResult delete(DocReportServiceLock docReportServiceLock, UserDto userDto) {
        Date date = new Date();
        docReportServiceLock.setStatus(UNLOCK);
        docReportServiceLock.setIsDelete(1L);
        docReportServiceLock.setUnlockUserSoid(userDto.getEmployeeSoid());
        docReportServiceLock.setUnlockDateTime(date);
        docReportServiceLock.setUpdateUser(userDto.getEmployeeSoid());
        docReportServiceLock.setUpdateDate(date);
        return docReportServiceLockApi.delete(docReportServiceLock);
    }

    /**
     * 功能描述：批量查询锁
     *
     * @param list
     * @return java.util.List<com.mida.bms.report.entity.DocReportServiceLock>
     * @date 2021/8/11
     **/
    @Override
    public List<DocReportServiceLock> selectLockList(List<DocReportListVo> list) {
        List<DocReportServiceLock> docReportServiceLockList = new ArrayList<>();
        list.forEach(docReportListVo -> {
            //包装查询报告状态，审核，书写医生条件
            DocReportServiceLock docReportServiceLock = new DocReportServiceLock();
            docReportServiceLock.setStatus(LOCK);
            docReportServiceLock.setRequestSoid(docReportListVo.getRequestSoid());//申请单标识
            docReportServiceLock.setRegistrationSheetSoid(docReportListVo.getRegistrationFormSoid());//登记标识
            docReportServiceLock.setExamCheckSoid(docReportListVo.getOperatorSoid()); //检查标识
            docReportServiceLock.setPolicySoid(docReportListVo.getReportType());
            docReportServiceLockList.add(docReportServiceLock);
        });

        ListResult<List<DocReportServiceLock>> listListResult = docReportServiceLockApi.selectLockList(docReportServiceLockList);
        return listListResult.getData();
    }
}
